package com.tekj.cxqcb.view.Module.activity.Order;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.DoGetOrderByIdBean;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.view.Module.activity.ApprovalAmountActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;

/**
 * 订单详情
 */
public class CarOrderDetailsActivity extends BaseActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.img_new)
    ImageView imgNew;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_car_name)
    TextView tvCarName;
    @BindView(R.id.tv_jrfa)
    TextView tvJrfa;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_use_time)
    TextView tvUseTime;
    @BindView(R.id.ll_jjxx)
    LinearLayout llJjxx;
    @BindView(R.id.ll_pfje)
    LinearLayout llPfje;
    @BindView(R.id.ll_spjl)
    LinearLayout llSpjl;
    @BindView(R.id.ll_htxx)
    LinearLayout llHtxx;
    @BindView(R.id.ll_qkxx)
    LinearLayout llQkxx;
    @BindView(R.id.tv_lxkh)
    TextView tvLxkh;
    @BindView(R.id.tv_th)
    TextView tvTh;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_dkze)
    TextView tvDkze;
    @BindView(R.id.tv_ckze)
    TextView tvCkze;
    @BindView(R.id.tv_dkqs)
    TextView tvDkqs;
    @BindView(R.id.tv_ygje)
    TextView tvYgje;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_id)
    TextView tvId;

    String id;

    BusinessDao businessDao;
    public static DoGetOrderByIdBean.DataBean orderData;


    @Override
    public void init() {
        tvTitle.setText("订单详情");
        businessDao = new BusinessDao(activity, this);
        id = getIntent().getStringExtra("id");
        businessDao.DoGetOrderById(id);

    }

    @Override
    public int setLayout() {
        return R.layout.activity_car_order_details;
    }

    boolean isF = true;

    @Override
    public void initLoadStart() {
        if (isF) {
            loading.show();
            isF = false;
        } else {
            showDialog();
        }
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 购车订单详情:
                DoGetOrderByIdBean byIdBean = (DoGetOrderByIdBean) bindingViewBean.getBean();
                if (byIdBean.getCode().equals("0000000")) {
                    orderData = byIdBean.getData();
                    setData(orderData);
                }
                break;
            case 订单完成:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if (pubStatusBean.getCode().equals("0000000")) {
                    businessDao.DoGetOrderById(id);
                }
                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.ll_jjxx, R.id.ll_pfje,
            R.id.ll_spjl, R.id.ll_htxx, R.id.ll_qkxx,
            R.id.tv_lxkh, R.id.tv_th})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_jjxx:
                startActivity(new Intent(activity, NewOrdersActivity.class)
                        .putExtra("type", 1)
                        .putExtra("id", orderData.getOrder().getId())
                        .putExtra("userid", "123456789"));
                break;
            case R.id.ll_pfje:
                startActivity(new Intent(activity, ApprovalAmountActivity.class));
                break;
            case R.id.ll_spjl:

                break;
            case R.id.ll_htxx:
                startActivity(new Intent(activity, ContractInformationActivity.class));
                break;
            case R.id.ll_qkxx:
                startActivity(new Intent(activity, RequestInformationActivity.class));
                break;
            case R.id.tv_lxkh:
                Tools.callPhone(activity, orderData.getOrder().getUserPhone());
                break;
            case R.id.tv_th:
                if (orderData.getOrder().getOrderStatus() != 12) {
                    return;
                }
                Tools.showNormalDialogOne(activity, "订单完成", "订单是否完成", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        businessDao.UpdateByIdCompleted(orderData.getOrder().getId(), MainBActivity.User.getUserId());
                    }
                }, null);

//                Toasty.normal(activity, "功能暂未开放").show();
                break;
        }
    }

    /**
     * 设置
     *
     * @param byIdBean
     */
    void setData(DoGetOrderByIdBean.DataBean byIdBean) {
        tvType.setText(getOrderType(byIdBean.getOrder().getOrderStatus()));
        tvId.setText(byIdBean.getOrder().getId());

        tvName.setText(byIdBean.getOrder().getUserName());
        tvPhone.setText(byIdBean.getOrder().getUserPhone());

        tvCarName.setText(byIdBean.getDetail().getTitle());
        tvJrfa.setText("平安租赁");

        tvDkze.setText(byIdBean.getOrder().getTotalLoanAmount());
        tvCkze.setText(byIdBean.getDetail().getPlatformPrice());
        tvDkqs.setText(byIdBean.getPlan().getCycle());
        tvYgje.setText(byIdBean.getDetail().getMonPay());

        tvTime.setText("创建时间：" + byIdBean.getOrder().getCreateTime());

//        Tools.picassoNetwork(byIdBean.getDetail().getImage(), imgPhoto);
        tvTh.setEnabled(orderData.getOrder().getOrderStatus() == 12);
        tvTh.setBackgroundColor(getResources().getColor(
                orderData.getOrder().getOrderStatus() == 12 ? R.color.colorPrimary : R.color.dividerColor));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        businessDao.DoGetOrderById(id);
    }

    String getOrderType(int Status) {
        String type = "";
        switch (Status) {
            case 1:
                type = "审核中";
                break;
            case 2:
                type = "待补充资料";
                break;
            case 3:
                type = "待付款";
                break;
            case 4:
                type = "待付款";
                break;
            case 5:
//                type = "审核中";
                break;
            case 6:
                type = "待补充资料";
                break;
            case 7:
                type = "审核中";
                break;
            case 8:
                type = "待补充资料";
                break;
            case 9:
                type = "审核中";
                break;
            case 10:
                type = "提交放款单";
                break;
            case 11:
                type = "风控待审核";
                break;
            case 12:
                type = "待客户提车";
                break;
            case 13:
                type = "订单完成";
                break;
        }
        return type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
